﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTO;

namespace DAL
{
    public class TUKHOAPHUONGDAO : DAOBASE
    {
        public DataTable Search2(Search input)
        {
            DataTable final = new DataTable();
            String sql = "SELECT  DISTINCT MaPhuong  FROM TUKHOAPHUONG WHERE  INSTR('" + input.InPut + "', TuKhoaPhuong) > 0";
            final = ExcuteQuery(sql);
            return final;
        }  
        public DataTable Search(Search input)
        {
            DataTable final = new DataTable();
            try
            {
                //thuc hien 
                String[] s = Until.ConvertStringToArray2(input.InPut);
                DataColumn[] cl = new DataColumn[] { new DataColumn("MaPhuong") };
                final.Columns.AddRange(cl);
                for (int i = 0; i < s.Length; i++)
                {
                    String sql = "SELECT  DISTINCT MaPhuong  FROM TUKHOAPHUONG WHERE ";
                    int pos = 0;
                    if (i + 3 < s.Length)
                    {
                        pos += i + 3;
                    }
                    else
                    {
                        if (i + 3 == s.Length)
                        {
                            pos = s.Length - i;
                            if (pos < i)
                            {
                                pos = s.Length - 1;
                            }
                        }
                        if (i + 3 > s.Length)
                        {
                            pos = s.Length - 1;
                        }

                    }
                    sql += Until.CreateWhere2(s, "TuKhoaPhuong", i, pos);
                  //  sql += Until.CreateWhere(s, "TuKhoaPhuong");
                    DataTable dt = new DataTable();
                    dt = ExcuteQuery(sql);
                    if (dt.Rows.Count >= 0)
                    {
                        foreach (DataRow dr in dt.Rows)
                        {
                            Until.AddData(dr, ref final, "MaPhuong", "MaPhuong");
                        }
                    }
                }
            }
            catch
            {
                return null;
            }
            return final;

        }
    }
}
